package com.prim.threadcoreknowledge.stopthread;

/**
 * run方法内没有阻塞方法时停止线程
 */
public class RightWayStopThreadWithoutSleep implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();//interrupt只是通知了线程中断，但是线程是否要中断取决于线程本身。
    }

    @Override
    public void run() {
        int num = 0;
        //Thread.currentThread().isInterrupted()加上线程响应中断
        while (!Thread.currentThread().isInterrupted() && num <= Integer.MAX_VALUE / 2) {
            if (num % 10000 == 0) {
                System.out.println(num + "是10000的倍数");
            }
            num++;
        }
        System.out.println("任务执行结束");
    }
}
